package com.china08.yxyapi.api.manage.edu.platform.statistics.weikepandect;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.SystemConstant;
import com.china08.yxyapi.SystemConstant.SchoolType;
import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.statistics.SchoolUploadResourceReport;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.service.mg.WeiKeCountSchoolListService;

import io.swagger.annotations.ApiOperation;

/**
 * 教育局平台办公     微课统计    微课统计列表    学校
 * 
 */
@CrossOrigin
@RestController
public class WeikeCountSchoolListController extends YxyApiBaseController{
	@Autowired
	private WeiKeCountSchoolListService weiKeCountSchoolListService;
	@ApiOperation(value = "微课统计    微课统计列表    学校")
	@ResponseBody
	@RequestMapping(value = "/api_yw/manage/edu/platform/statistics/resourcepandect/countschoollist2/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Page<RespModel> precess(HttpServletRequest request, HttpServletResponse response,@RequestBody ReqModel reqModel) {
			Integer schoolBureauLevel =getProfile(request, response).getSchoolBureauLevel();
	        String districtCode=null;
	        Pageable pageable = new PageRequest(reqModel.page, reqModel.size);
	        String phaseId=null;
	        String phaseName = reqModel.phaseName;
	        if (phaseName!=null) {
	        	if (phaseName.equals("幼儿园")) {
		        	phaseId="ST0";
				}
		        if (phaseName.equals("小学")) {
		        	phaseId="ST1";
				}
		        if (phaseName.equals("初中")) {
		        	phaseId="ST2";
				}
		        if (phaseName.equals("高中")) {
		        	phaseId="ST3";
				}
		        if (phaseName.equals("大学")) {
		        	phaseId="ST4";
				}
		        if (phaseName.equals("培训机构")) {
		        	phaseId="ST5";
				}
			}
	        
	        Page<SchoolUploadResourceReport>  schoolUploadResourceReportLst=null;
	        if (schoolBureauLevel==0) {//区
	        	districtCode=getProfile(request, response).getSchoolAreaIdDistrict();
	        	schoolUploadResourceReportLst=weiKeCountSchoolListService.getAreaContent(districtCode, reqModel.cityName, reqModel.areaName, phaseId, reqModel.schoolName, reqModel.listOrder, pageable);
			}else if(schoolBureauLevel==1){//市
				districtCode=getProfile(request, response).getSchoolAreaIdCity();
				schoolUploadResourceReportLst=weiKeCountSchoolListService.getCityContent(districtCode, reqModel.cityName, reqModel.areaName, phaseId, reqModel.schoolName, reqModel.listOrder, pageable);
			}else if(schoolBureauLevel==2){//省
				districtCode=getProfile(request, response).getSchoolAreaIdProvince();
				schoolUploadResourceReportLst=weiKeCountSchoolListService.getProvinceContent(districtCode, reqModel.cityName, reqModel.areaName, phaseId, reqModel.schoolName, reqModel.listOrder, pageable);
			}
	        
	        RespModel model=null;
	        List<RespModel> modelLst=new ArrayList<RespModel>();
	        Integer order=0;
	        //便利查询到的最终数据
	        for (SchoolUploadResourceReport schoolUploadResourceReport : schoolUploadResourceReportLst) {
	        	order+=1;
	        	model=new RespModel();
	        	if (schoolUploadResourceReport.getSchoolNick()==null) {					
	        		model.schoolNick="";
				}else{
					model.schoolNick=schoolUploadResourceReport.getSchoolNick();
				}
	        	if (schoolUploadResourceReport.getStage()!=null && schoolUploadResourceReport.getStage().size()>0) {
	        		String stageName = "";
	        		for (String schoolUpload : schoolUploadResourceReport.getStage()) {						
	        			stageName+=SystemConstant.SchoolType.getDescription(SchoolType.valueOf(schoolUpload))+",";
					}
	        		model.stages=stageName.substring(0, stageName.length()-1);
				}else{
					model.stages="";
				}
	        	if (schoolUploadResourceReport.getAreaNameCity()==null) {					
	        		model.areaNameCity = "";
				}else{
					model.areaNameCity = schoolUploadResourceReport.getAreaNameCity();
				}
	        	if (schoolUploadResourceReport.getAreaNameDistrict()==null) {
	        		model.areaNameDistrict ="";
				}else{					
					model.areaNameDistrict=schoolUploadResourceReport.getAreaNameDistrict();
				}
	        	if (schoolUploadResourceReport.getWeikeCount()==null) {					
	        		model.weikeCount = 0L;
				}else{
					model.weikeCount=schoolUploadResourceReport.getWeikeCount();
				}
	        	if (schoolUploadResourceReport.getShareWeikeCount()==null) {					
	        		model.shareWeikeCount = 0L;
				}else{
					model.shareWeikeCount =schoolUploadResourceReport.getShareWeikeCount();
				}
	        	model.order=order;
	            modelLst.add(model);
			}
	        
	        
//	        List<RespModel> schoolUploadResourceReportLst2 =new ArrayList<RespModel>();
//	        if (modelLst.size()>10) {
//	        	//进行封装（每页数据不能超过10条）
//	        	schoolUploadResourceReportLst2 = modelLst.subList((pageable.getPageNumber()-1)*pageable.getPageSize(), 10);
//			}else{
//				schoolUploadResourceReportLst2 = modelLst;
//			}
	        
	        
	        PageImpl<RespModel> pageImpl=new PageImpl<RespModel>(modelLst,pageable,schoolUploadResourceReportLst.getTotalElements());
			Page<RespModel> pageLst = pageImpl;
	        return pageLst;
    }
    protected static class RespModel extends YxyApiRespModel {
    	//序号
    	public Integer order;
    	//学校名称
    	public String schoolNick;
    	//学段名称(拼接的字符串)
    	public String stages;
    	//市名称
    	public String areaNameCity;
    	//区县名称
    	public String areaNameDistrict;
    	//微课总数
    	public Long weikeCount;
    	//微课共享总数
    	public Long shareWeikeCount;
    }
    
    protected static class ReqModel extends YxyApiPageReqModel {
    	//选择市
    	public String cityName;
    	//选择区县
    	public String areaName;
    	//选择学段
    	public String phaseName;
    	//学校名称
    	public String schoolName;
    	//列表排序
    	public String listOrder;
    }
    
}